package com.project.servlet;

import com.project.sevice.impl.StudentController;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/7/20 13:58
 */
@Controller
public class StudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取请求路径
        String path=req.getServletPath();
        path=path.substring(1,path.lastIndexOf("."));
        Class c=StudentController.class;
        try {
            //根绝URL请求查找处理的方法
            Method m=c.getMethod(path,HttpServletRequest.class,HttpServletResponse.class);
            //实例化对象
            Object Controller=c.getConstructor().newInstance();
            //执行方法
            m.invoke(Controller,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
